<?php
namespace app\controller;

use think\Controller;
use think\Db;

class MobileBase extends Controller {
    public $code;

    public function encoded_code($data){
        if($data){
            $this->code = $data;
        }else{
            $this->code = 'zh-cn';
        }

    }
    public function ajaxReturn($data){
        $data['msg'] = $this->language($data['code'],$this->code);
        exit(json_encode($data));
    }
    public function language($status,$code)
    {
        $data = array(
            0=>array('zh-cn'=>'数据异常','en'=>'Data abnormity','ja'=>'データ異常','ko'=>'데이터 에러'),
            200=>array('zh-cn'=>'成功','en'=>'success','ja'=>'成功','ko'=>'성공'),
            100=>array('zh-cn'=>'失败！！！','en'=>'error','ja'=>'種子が足りない','ko'=>'종자가 부족하다'),
            197=>array('zh-cn'=>'帐号不存在','en'=>'success','ja'=>'成功','ko'=>'성공'),
            198=>array('zh-cn'=>'无效帐号','en'=>'success','ja'=>'成功','ko'=>'성공'),
            199=>array('zh-cn'=>'密码错误','en'=>'success','ja'=>'成功','ko'=>'성공'),
            201=>array('zh-cn'=>'由于广告被广告方修改，该广告已失效','en'=>'success','ja'=>'成功','ko'=>'성공'),
            202=>array('zh-cn'=>'暂无最新新闻动态','en'=>'success','ja'=>'成功','ko'=>'성공'),
            203=>array('zh-cn'=>'请选择链','en'=>'success','ja'=>'成功','ko'=>'성공'),
            110001=>array('zh-cn'=>'参数已过过期','en'=>'success','ja'=>'成功','ko'=>'성공'),
            110002=>array('zh-cn'=>'账户验证异常','en'=>'success','ja'=>'成功','ko'=>'성공'),
        );

        return $data[$status][$code];   
    }
    function check_user()
    {
        $data = request()->param();
        // $token = Request::instance()->header('token');
        if($data['token']){
            $redis = new Redis();
            $userInfo = $redis->get($data['token']);
            $userInfo = json_decode($userInfo,true);
            if(!$userInfo){
                return ['code'=>110004,'msg'=>'该用户未登陆'];
            }
            if(strtotime($userInfo['last_time']) + 24*3600*30 > time()){
                return ['code'=>110005,'msg'=>'长时间未登陆，请先登陆'];
            }
            if($userInfo['is_status'] != 1){
                return ['code'=>110006,'msg'=>'该账户已被禁止登陆，请联系客服解封'];
            }
        }
        // 验证时间
        $times = time();
        if ($data['time'] + 300 < $times) {
          return ['code' => 110002, 'msg' => '参数已过过期!'];
        }
        //按照键的第一个字母排序
        $code = $data['sign'];
        unset($data['sign']);  
        $keyArr = array_keys($data);
        unset($keyArr[0]);
        natsort($keyArr);
        $keyArray = array_values($keyArr);
        foreach ($keyArray as $k => $v) {
          $arr[$k] = $data[$v];
        }
        $str = implode('', $arr);
        $key = 'zyxfdbs';
        $key2 = 'ydjkzbh';
        $sign = md5(md5($str.$key).$key2);
        // 账证账户
        if ($sign != $code) {
            return ['code' => 110003, 'msg' => '账户验证异常!'];
        }
  
        return true;
    }
    public function curl_post($url,$data=array()){

        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        // POST数据

        curl_setopt($ch, CURLOPT_POST, 1);

        // 把post的变量加上

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;

    }

     function curl_get($url,$postdata=[]) {
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL, $url);
		// if ($this->req_method == 'POST') {
		// 	curl_setopt($ch, CURLOPT_POST, 1);
		// 	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata));
		// }
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_TIMEOUT,60);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
		curl_setopt ($ch, CURLOPT_HTTPHEADER, [
			"Content-Type: application/json",
			]);
		$output = curl_exec($ch);
        $info = curl_getinfo($ch);
        if (curl_error($ch)) {
            print "Error: " . curl_error($ch);
        }
		curl_close($ch);
		return $output;
	}
}